#include <QDebug>
#include <QDesktopWidget>
#include <QPoint>
#include <main_window.h>

MainWindow::MainWindow(QWidget* parent)
	: QMainWindow(parent)
{
	setupUi(this);
	txtProd0->setValidator(new QDoubleValidator(txtProd0));
	txtProd1->setValidator(new QDoubleValidator(txtProd1));
	txtProd2->setValidator(new QDoubleValidator(txtProd2));
	txtProd3->setValidator(new QDoubleValidator(txtProd3));
	txtProd4->setValidator(new QDoubleValidator(txtProd4));
	txtProd5->setValidator(new QDoubleValidator(txtProd5));
	txtSave0->setValidator(new QDoubleValidator(txtSave0));
	txtSave1->setValidator(new QDoubleValidator(txtSave1));
	txtSave2->setValidator(new QDoubleValidator(txtSave2));
	txtSave3->setValidator(new QDoubleValidator(txtSave3));
	txtSave4->setValidator(new QDoubleValidator(txtSave4));
	txtSave5->setValidator(new QDoubleValidator(txtSave5));
}

void MainWindow::calculate()
{
	if (isFieldsNotEmpty() && isFieldsHavePositiveValues()) {
		board = new PresentationWindow(this,
																	getProdactionCosts(),
																	getStorageExpenses());
		this->hide();
		board->moveToCenter();
		board->show();
	} else {
		QMessageBox msg;
		msg.setText(trUtf8("   Не верное значение в одном из полей. \
Поля должны быть не пустыми и иметь положительные значения"));
		msg.exec();
	}
}

void MainWindow::moveToCenter()
{
	QPoint center = QDesktopWidget().availableGeometry().center();
	move(center - rect().center());
}

void MainWindow::clearFields()
{
	txtProd0->setText("");
	txtProd1->setText("");
	txtProd2->setText("");
	txtProd3->setText("");
	txtProd4->setText("");
	txtProd5->setText("");
	txtSave0->setText("");
	txtSave1->setText("");
	txtSave2->setText("");
	txtSave3->setText("");
	txtSave4->setText("");
	txtSave5->setText("");
}

QVector<qreal> MainWindow::getProdactionCosts()
{
	QVector<qreal> res;
	res << txtProd0->text().toDouble();
	res << txtProd1->text().toDouble();
	res << txtProd2->text().toDouble();
	res << txtProd3->text().toDouble();
	res << txtProd4->text().toDouble();
	res << txtProd5->text().toDouble();
	return res;
}

QVector<qreal> MainWindow::getStorageExpenses()
{
	QVector<qreal> res;
	res << txtSave0->text().toDouble();
	res << txtSave1->text().toDouble();
	res << txtSave2->text().toDouble();
	res << txtSave3->text().toDouble();
	res << txtSave4->text().toDouble();
	res << txtSave5->text().toDouble();
	return res;
}

bool MainWindow::isFieldsNotEmpty() {
	if (txtSave0->text() != "" &&
			txtSave1->text() != "" &&
			txtSave2->text() != "" &&
			txtSave3->text() != "" &&
			txtSave4->text() != "" &&
			txtSave5->text() != "" &&
			txtProd0->text() != "" &&
			txtProd1->text() != "" &&
			txtProd2->text() != "" &&
			txtProd3->text() != "" &&
			txtProd4->text() != "" &&
			txtProd5->text() != "") {
				return true;
			} else {
				return false;
			}
}

bool MainWindow::isFieldsHavePositiveValues() {
	if (txtSave0->text().toDouble() >= 0 &&
			txtSave1->text().toDouble() >= 0 &&
			txtSave2->text().toDouble() >= 0 &&
			txtSave3->text().toDouble() >= 0 &&
			txtSave4->text().toDouble() >= 0 &&
			txtSave5->text().toDouble() >= 0 &&
			txtProd0->text().toDouble() >= 0 &&
			txtProd1->text().toDouble() >= 0 &&
			txtProd2->text().toDouble() >= 0 &&
			txtProd3->text().toDouble() >= 0 &&
			txtProd4->text().toDouble() >= 0 &&
			txtProd5->text().toDouble() >= 0) {
				return true;
			} else {
				return false;
			}
}
